'Populates the IndividualDisclosure with the OutsideEntity Name - AnswerFiller - VRG4
Option Strict Off
imports System
imports PHSRAG.Insight21.Humans
imports PHSRAG.Insight21.BusinessObjects
imports PHSRAG.Insight21.FormsManagement
imports COI = PHSRAG.Insight21.COI
imports System.Collections

module Script
    sub Main()				
				
		'Update Individual Disclosure
		currentCOI.LoadQuestionnaireAnswers()
		dim questionnaireAnswers as ArrayList = currentCOI.QuestionnaireAnswers
                dim initialQuestionnaireAnswer as QuestionnaireAnswer

		For Each questionnaireAnswer as QuestionnaireAnswer in questionnaireAnswers
			If (questionnaireAnswer.QuestionnaireCategory = PHSRAG.Insight21.FormsManagement.Constants.COICategory.InitialQuestionnaire) Then
				
					initialQuestionnaireAnswer = questionnaireAnswer

					dim parentAnswerList as ArrayList = questionnaireAnswer.GetAnswers("OutsideActivity")

					if parentAnswerList.Count > 0 Then						
						dim answers as ArrayList = questionnaireAnswer.GetAnswers("ListOutsideActivities")
						if not answers is Nothing Then
							For Each answer as Answer in answers								
								answer.MarkForDelete = true
							Next
						End If
						' Set to N because OA could be set to Y, but the child answers were N, so it didn't rise to the level
						dim parentAnswer as Answer = parentAnswerList(0)
						parentAnswer.Value = "N"
					End If
					parentAnswerList = nothing
					parentAnswerList = questionnaireAnswer.GetAnswers("YourEquityInterests")

					if parentAnswerList.Count > 0 Then						
						dim answers as ArrayList = questionnaireAnswer.GetAnswers("ListEquityInterests")
						if not answers is Nothing Then
							For Each answer as Answer in answers								
								answer.MarkForDelete = true							
								
							Next
						End If
					End If
					parentAnswerList = nothing
					parentAnswerList  = questionnaireAnswer.GetAnswers("YourFamily")

					if parentAnswerList.Count > 0 Then						
						dim answers as ArrayList = questionnaireAnswer.GetAnswers("ListFamilyAssociations")
						if not answers is Nothing Then
							For Each answer as Answer in answers
								answer.MarkForDelete = true								
								
							Next
						End If
					End If
					
					parentAnswerList = nothing
					parentAnswerList  = questionnaireAnswer.GetAnswers("YourFamilyEquity")

					if parentAnswerList.Count > 0 Then						
						dim answers as ArrayList = questionnaireAnswer.GetAnswers("ListFamilyEquity")
						if not answers is Nothing Then
							For Each answer as Answer in answers
								answer.MarkForDelete = true								
							Next
						End If
					End If
					initialQuestionnaireAnswer.Save(connectionString)
					exit for
			End If
		Next

		For Each questionnaireAnswer as QuestionnaireAnswer in questionnaireAnswers

			If (questionnaireAnswer.QuestionnaireCategory = PHSRAG.Insight21.FormsManagement.Constants.COICategory.OutsideActivity) Then
				
				dim parentAnswerList as ArrayList = initialQuestionnaireAnswer.GetAnswers("OutsideActivity")
				if parentAnswerList.Count > 0 Then
					dim parentAnswer as Answer = parentAnswerList(0)
					' Since there is an OA questionnaire, set OA to Y
					parentAnswer.Value = "Y"
					dim answer as new Answer(cachePolicy)
					answer.Name = "ListOutsideActivities"
					answer.Value = string.Format("{0}|{1}", questionnaireAnswer.ExternalId.ToString(), questionnaireAnswer.Name.ToString())
					answer.QuestionnaireAnswerId = questionnaireAnswer.Id 
					answer.ParentAnswerId = parentAnswer.Id					
					initialQuestionnaireAnswer.Answers.Add(answer)
					
				End If

			Else If (questionnaireAnswer.QuestionnaireCategory = PHSRAG.Insight21.FormsManagement.Constants.COICategory.EquityInterest) Then

				dim parentAnswerList as ArrayList = initialQuestionnaireAnswer.GetAnswers("YourEquityInterests")
				if parentAnswerList.Count > 0 Then
					dim parentAnswer as Answer = parentAnswerList(0)
					dim answer as new Answer(cachePolicy)
					answer.Name = "ListEquityInterests"
					answer.Value = string.Format("{0}|{1}", questionnaireAnswer.ExternalId.ToString(), questionnaireAnswer.Name.ToString())
					answer.QuestionnaireAnswerId = questionnaireAnswer.Id 
					answer.ParentAnswerId = parentAnswer.Id					
					initialQuestionnaireAnswer.Answers.Add(answer)					

				End If
			Else If (questionnaireAnswer.QuestionnaireCategory = PHSRAG.Insight21.FormsManagement.Constants.COICategory.FamilyAssociation) Then

				dim parentAnswerList as ArrayList = initialQuestionnaireAnswer.GetAnswers("YourFamily")
				if parentAnswerList.Count > 0 Then
					dim parentAnswer as Answer = parentAnswerList(0)
					dim answer as new Answer(cachePolicy)
					answer.Name = "ListFamilyAssociations"
					answer.Value = string.Format("{0}|{1}", questionnaireAnswer.ExternalId.ToString(), questionnaireAnswer.Name.ToString())
					answer.QuestionnaireAnswerId = questionnaireAnswer.Id 
					answer.ParentAnswerId = parentAnswer.Id
					initialQuestionnaireAnswer.Answers.Add(answer)
					
				End If
			End If
		Next
		initialQuestionnaireAnswer.Save(connectionString)
						

				
		

    end sub
end module